package com.yds.developtools.views

import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.yds.developtools.utils.YDSActivityStackHelper
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 作者:     YDS
 * 创建时间: 2024/9/23 11:03
 * 页面描述: QMUI弹窗帮助类
 */
object QMUIDialogHelper {

    /*** 展示提示对话框  */
    fun showTipsDialog(title: String?, message: String?, confirm: (Boolean) -> Unit) {
        QMUIDialog.MessageDialogBuilder(YDSActivityStackHelper.topActivity())
            .setMessage(message)
            .setTitle(title)
            .addAction(
                "确认"
            ) { dialog, _ ->
                dialog.dismiss()
                confirm(true)
            }
            .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show()
    }

    /*** 展示提示对话框  */
    fun showTipsDialog(title: String?, message: String?, confirm: (Boolean) -> Unit, cancel: (Boolean) -> Unit) {
        QMUIDialog.MessageDialogBuilder(YDSActivityStackHelper.topActivity())
            .setMessage(message)
            .setTitle(title)
            .addAction("确认") { dialog, _ ->
                dialog.dismiss()
                confirm(true)
            }
            .addAction("取消") { dialog, _ ->
                dialog.dismiss()
                cancel(true)
            }
            .create(com.qmuiteam.qmui.R.style.QMUI_Dialog).show()
    }

    private var tipDialog: QMUITipDialog? = null

    /**
     * QMUI弹窗
     *
     * @param tipsType 1、QMUITipDialog.Builder.ICON_TYPE_LOADING 数据加载中...
     * 2、QMUITipDialog.Builder.ICON_TYPE_SUCCESS 发送成功
     * 3、QMUITipDialog.Builder.ICON_TYPE_FAIL 发送失败
     * 4、QMUITipDialog.Builder.ICON_TYPE_INFO 请勿重复操作
     * @param tipsMsg
     */
    fun showQMUIDialog(tipsType: Int, tipsMsg: String?, autoDismiss: Boolean = true, listener: OnDismissCompleteListener? = null) {
        dismissQMUIDialog()
        tipDialog = QMUITipDialog.Builder(YDSActivityStackHelper.topActivity())
            .setIconType(tipsType)
            .setTipWord(tipsMsg)
            .create()
        tipDialog?.setCancelable(false)
        tipDialog?.setCanceledOnTouchOutside(false)
        tipDialog?.show()

        if (autoDismiss) {
            GlobalScope.launch {
                delay(1500)
                dismissQMUIDialog()
                listener?.onComplete()
            }
        }
    }

    interface OnDismissCompleteListener {
        fun onComplete()
    }

    /**
     * 关闭QMUI弹窗
     */
    fun dismissQMUIDialog() {
        tipDialog?.dismiss()
    }

    fun dismissQMUIDialogDelay() {
        GlobalScope.launch {
            delay(500)
            tipDialog?.dismiss()
        }
    }


}